import warnings
import requests
import urllib3
from bs4 import BeautifulSoup


def loginIspace():
    username = input("Username: ")
    pwd = input("Passward: ")

    loginUrl= "https://ispace.uic.edu.hk/login/index.php"

    session = requests.Session()

    html_get = session.get(loginUrl, verify=False)

    soup = BeautifulSoup(html_get.text, 'html.parser').find("input",{'name': 'logintoken'})['value']
    print(soup)

    postdata = {
        'anchor': "",
        'logintoken': soup,
        # 'username': "username",
        # 'password': "password"
        'username': username,
        'password': pwd
    }

    header = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0",
        'Referer': loginUrl
    }

    # fixed "Unverified HTTPS request is being made to xxxx" warning
    urllib3.disable_warnings()
    requests.packages.urllib3.disable_warnings()
    warnings.filterwarnings('ignore')

    html_post = session.post(loginUrl, postdata, headers=header, verify=False)

    # print(html_post.url)

    # print(html_post.text)
    return session, html_post


def saveLoginHTML(html_post):
    # save logined html
    save_based = "savedHTML\\"
    save_path = input("Save Name: ")
    save_path_full = save_based + save_path
    # save = open("save.html","wb")
    save = open(save_path_full,"wb")
    save.write(html_post.text.encode())
    save.close()


def main():
    html_post = loginIspace()
    #print(html_post.text)


if __name__ == "__main__":
    main()
